
package com.example.hellodice;

import us.dicepl.android.sdk.BluetoothManipulator;
import us.dicepl.android.sdk.DiceConnectionListener;
import us.dicepl.android.sdk.DiceController;
import us.dicepl.android.sdk.DiceResponseAdapter;
import us.dicepl.android.sdk.DiceResponseListener;
import us.dicepl.android.sdk.DiceScanningListener;
import us.dicepl.android.sdk.Die;
import us.dicepl.android.sdk.responsedata.RollData;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView textView;
	private static final String TAG = "DICE+";
	
	public static final int[] developerKey = new int[]{0x83, 0xed, 0x60, 0x0e, 0x5d, 0x31, 0x8f, 0xe7};
	private Die dicePlus;
	
	
	DiceScanningListener scanningListener = new DiceScanningListener() {
	    @Override
	    public void onNewDie(Die die) {
	    	Log.d(TAG, "NEW DIE");
	        dicePlus = die;
	        DiceController.connect(dicePlus);
	    }

	    @Override
	    public void onScanStarted() {}

	    @Override
	    public void onScanFailed() {
	    	Log.d(TAG, "SCAN FAIL");
	        BluetoothManipulator.startScan();
	    }

	    @Override
	    public void onScanFinished() {
	        if(dicePlus == null) {
	            BluetoothManipulator.startScan();
	        }
	    }
	};
	
	DiceConnectionListener connectionListener = new DiceConnectionListener() {
	    @Override
	    public void onConnectionEstablished(Die die) {
	        Log.d(TAG, "DICE+ Connected");

	        // Signing up for roll events
	        DiceController.subscribeRolls(dicePlus);
	    }

	    @Override
	    public void onConnectionFailed(Die die, Exception e) {
	        Log.d(TAG, "Connection failed", e);

	        dicePlus = null;

	        BluetoothManipulator.startScan();
	    }

	    @Override
	    public void onConnectionLost(Die die) {
	        Log.d(TAG, "Connection lost");

	        dicePlus = null;

	        BluetoothManipulator.startScan();
	    }
	};
	
	DiceResponseListener responseListener = new DiceResponseAdapter() {
	    @Override
	    public void onRoll(Die die, RollData rollData, Exception e) {
	        super.onRoll(die, rollData, e);

	        Log.d("DICEPlus", "Roll: " + rollData.face);
	        
	        final int face = rollData.face;

	        MainActivity.this.runOnUiThread(new Runnable() {
	            @Override
	            public void run() {
	                textView.setText(""+face);
	            }
	        });    
	    }
	};
	
	@Override
	protected void onResume() {
	    super.onResume();

	    textView = (TextView) findViewById(R.id.hello_world);

	    // Initiating
	    BluetoothManipulator.initiate(this);
	    DiceController.initiate(developerKey);

	    // Listen to all the state occurring during the discovering process of DICE+
	    BluetoothManipulator.registerDiceScanningListener(scanningListener);

	    // When connecting to DICE+ you get two responses: a good one and a bad one ;)
	    DiceController.registerDiceConnectionListener(connectionListener);

	    // Attaching to DICE+ events that we subscribed to.
	    DiceController.registerDiceResponseListener(responseListener);

	    // Scan for a DICE+
	    BluetoothManipulator.startScan();
	}
	
	@Override
	protected void onStop() {
	    super.onStop();

	    Log.d(TAG, "onStop");

	    DiceController.unregisterDiceConnectionListener(connectionListener);
	    BluetoothManipulator.unregisterDiceScanningListener(scanningListener);
	    DiceController.unregisterDiceResponseListener(responseListener);

	    DiceController.disconnectDie(dicePlus);
	    dicePlus = null;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textView = (TextView) findViewById(R.id.hello_world);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
